package com.example.amoxicilin.null_test.tools;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;

import java.io.File;

public class FileUpload {
	
	public static String UpLoadFile ( String str, String type, int id ) {
		
		int status = 0;            //状态码
		String targetURL = null ;//指定URL  
        File targetFile = null ;//指定上传文件  
        String response = "";  //返回值
        
        
        targetFile = new File ( str) ;  
        targetURL = "http://120.24.94.172/JsonTestServer/fileUpload?type=" + type + "&id=" + id; // servleturl
        PostMethod filePost = new PostMethod ( targetURL ) ;  
        try {  

        	Part [ ] parts = { new FilePart ( targetFile.getName ( ) , targetFile ) } ;  
        	filePost.setRequestEntity ( new MultipartRequestEntity (parts, filePost.getParams( )));  
        	HttpClient client = new HttpClient ( ) ;  
        	client.getHttpConnectionManager( ).getParams( ).setConnectionTimeout ( 5000 );  
        	status = client.executeMethod ( filePost ) ;
        	response = filePost.getResponseBodyAsString();     //获得返回数据
        	} catch ( Exception ex ) {
        		ex.printStackTrace ( );
            } finally {  
            	filePost.releaseConnection ( );  
            	}  
        return response;
        }  
}
